/**
 * @angular 管理员
 * @description
 * @auth yang li
 */

admin = function ($scope, $ducTools, $compile, http, $dataTableSet) {
    'use strict';
    var userTable = $('#userTable').DataTable({
        sAjaxSource: 'system/user/query',
        fnServerData: $ducTools.retrieveData,
        fnServerParams: function (aoData) {
            aoData.push(
                {"name": "userName", "value": $scope.userNameS}
            )
        },
        columns: [
            {"data": "id"},
            {"data": "userName"},
            {"data": "realName"},
            {"data": "roleName"},
            {"data": "email"},
            {"data": null},
            {"data": null},
        ],
        columnDefs: [
            {
                targets: 5,
                render: function (c) {
                    if (c.optlock == 0) {
                        return "<span class='btn btn-success btn-xs'>启用</span>";
                    } else if (c.optlock == 1) {
                        return "<span class='btn btn-primary btn-xs'>关闭</span>";
                    }
                }
            }, {
                targets: 6,
                render: function (c) {
                    return "<button class='btn btn-info btn-xs' ng-click=update('" + c.id + "')> 修改 </button> &nbsp;";
                }
            }
        ],
        createdRow: function (row, data, dataIndex) {
            $compile(row)($scope);
        }
    });

    $dataTableSet.dataTableRowShow('userTable');

    $scope.accountRecordDraw = function () {
        userTable.draw();
    };

    //获得角色
    http.post({"pageSize": 999999, "pageNo": 1}, 'system/role/query').then(function (data) {
        var $menusp = $("#menusp");
        $menusp.html("");
        for (var i = 0; i < data.length; i++) {
            var result = data[i];
            $menusp.append(
                "<li class= 'authList'>" +
                "<input name='" + result.roleName + "' class='checkboxAll' type='checkbox' value=" + result.id + " />"
                + result.roleName +
                "</li>"
            );
        }
        $compile($("#menusp li"))($scope);
    })
    $scope.actionAll = function ($event) {
        var allSelect = $("#menusp").find("input");
        $($event.target).prop("checked") ? allSelect.prop("checked", true) : allSelect.prop("checked", false);
    };

    //添加修改
    $scope.addUser = function (id) {
        $scope.adminId = "";
        $(id).modal('show').find("form")[0].reset();
    };
    $scope.update = function (id) {
        var menuspIds = [];
        $("#adminListModal3").modal('show');
        $scope.adminId = id;
        $("#menusp input").prop("checked", false);
        $scope.password = "";
        http.post({"id": id}, 'system/user/get').then(function (res) {
            $scope.userName = res.userName;
            //$scope.password = password;
            $scope.email = res.email;
            $scope.optlock = res.optlock;
            $scope.realName = res.realName;
            $scope.roleId = res.roleId;
            for (var i = 0; i < res.roleIds.length; i++) {
                $("#menusp input[value= " + res.roleIds[i] + "]").prop("checked", true);
            }
        });
    }
    $("#addAdminForm3").validate({
        rules: {
            userName: 'required',
            roleId: 'required'
        },
        messages: {
            userName: {
                required: "请输入用户名！"
            },
            roleId: {
                required: "请选择角色！"
            }
        },
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function () {
            var _url;
            var data;

            var checkeds = $("input.checkboxAll:checked"),
                roleIds = "";

            for (var i = 0; i < checkeds.length; i++) {
                if (i == (checkeds.length * 1 - 1)) {
                    roleIds += checkeds.eq(i).val();
                } else {
                    roleIds += checkeds.eq(i).val() + ",";
                }
            }

            if ($scope.adminId == "") {
                _url = 'system/user/add'
                data = {
                    "userName": $scope.userName,
                    "password": $scope.password,
                    "email": $scope.email,
                    "optlock": $scope.optlock,
                    "realName": $scope.realName,
                    "roleIds": roleIds,
                };
            } else {
                _url = 'system/user/mod'
                data = {
                    "id": $scope.adminId,
                    "userName": $scope.userName,
                    "password": $scope.password,
                    "email": $scope.email,
                    "optlock": $scope.optlock,
                    "realName": $scope.realName,
                    "roleIds": roleIds,
                };
            }
            http.post(data, _url).then(function (result) {
                $('#adminListModal3').modal('hide');
                swal("保存成功！")
                userTable.draw();
            })
        },
        errorPlacement: function (error, element) {
            var $addAdminFormErrorMsg = $("#addAdminFormErrorMsg");
            if (!$addAdminFormErrorMsg.find('label').is(':visible')) {
                $addAdminFormErrorMsg.html(error);
            }
            return false;
        }
    });

};
